﻿using MiniFox.Components;
using MiniFox.Configurations;
using MiniFox.Log;
using MiniFox.Web;
using System.Xml.Linq;
using Wechat.Core;
using Wechat.Core.Models;
using Wechat.Corp.Configurations;
using Wechat.Corp.Log;

namespace Wechat.Corp
{
    /// <summary>
    /// 
    /// </summary>
    public abstract class WechatAgentService : WechatServiceBase, IWechatAgentService
    {
        private string _name;
        private WechatCorpServiceConfiguration _serverConfig;
        private IWechatCorpConfiguration _secureConfig;
        private IWechatAgent _setting;

        /// <summary>
        /// 
        /// </summary>
        public WechatAgentService() : this(string.Empty)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public WechatAgentService(string name)
        {
            this._name = name;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public WechatAgentService(IWechatAgent setting)
        {
            this._name = setting.Name;
            this._setting = setting;
        }

        /// <summary>
        /// 
        /// </summary>
        [AutoWired]
        protected WechatCorpConfigProvider ConfigProvider
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        protected virtual WechatCorpServiceConfiguration ServerConfig
        {
            get
            {
                if (this._serverConfig == null)
                    _serverConfig = ConfigurationFactory.GetConfig<WechatCorpServiceConfiguration>();
                return _serverConfig;
            }
        }

        private IWechatCorpConfiguration SecureConfiguration
        {
            get
            {
                if (this._secureConfig == null)
                {
                    _secureConfig = this.ConfigProvider.GetSecureConfig();
                }
                return _secureConfig;
            }
        }

        #region 
        /// <summary>
        /// 
        /// </summary>
        public override string Name
        {
            get
            {
                return this._name;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string CorpID
        {
            get
            {
                return SecureConfiguration.CorpID;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string AgentID
        {
            get
            {
                return Setting.AgentID;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string SecretKey
        {
            get
            {
                return Setting.SecretKey;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override string Token
        {
            get
            {
                return Setting.Token;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public string AesKey
        {
            get
            {
                return Setting.AesKey;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override Uri AccessTokenUri
        {
            get
            {
                return new Uri(ServerConfig.AccessTokenUri);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IWechatAgent Setting
        {
            get
            {
                if (this._setting == null)
                {
                    this._setting = ConfigProvider.GetAgentSetting(this.Name);
                }
                return this._setting;
            }
        }

        #endregion

        #region access token

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override Task<AccessTokenResponse> GetAccessTokenAsync()
        {
            var uri = this.AccessTokenUri.AddQueryString(new { corpid = this.CorpID, corpsecret = Setting.SecretKey });
            return HttpGetAsync<AccessTokenResponse>(uri);
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="method"></param>
        /// <param name="requestData"></param>
        /// <param name="responseData"></param>
        /// <param name="elapsed"></param>
        protected override void WriteLog(string url, string method, string requestData, string responseData, int elapsed)
        {
            if (string.IsNullOrEmpty(this.ServerConfig.LoggerName))
                return;
            var factory = ComponentFactory.GetComponent<LoggerFactory>();
            ILogger logger = factory.CreateLogger(this.ServerConfig.LoggerName);

            logger.WriteInfo(new WechatServiceLogContent
            {
                CorpID = this.CorpID,
                AgentID = this.AgentID,
                SecretKey = this.SecretKey,
                HttpMethod = method,
                RequestUri = url,
                Elapsed = elapsed,
                RequestData = requestData,
                ResponseData = responseData
            });
        }
        
    }
}

